﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hockey.Shared;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.ViewModel;
using MVVMDialogs;

namespace Hockey.UILayer.ViewModels
{
    /// <summary>
    /// ViewModel for new game form
    /// </summary>
    internal class NewGameFormVM: NotificationObject
    {
        private GameSettings _gameSettings;
        private IEventAggregator _events;

        /// <summary>
        /// Returns game settings
        /// </summary>
        public GameSettings GameSettings
        {
            get
            {
                return _gameSettings;
            }
        }

        
        /// <summary>
        /// If terminated form with success
        /// </summary>
        public bool Success
        { get; set; }

        public NewGameFormVM(IEventAggregator events)
        {
            if (events == null)
                throw new ArgumentNullException("Events");
            _events = events;

            // set default settings
            _gameSettings.GameMode = eGameMode.ComputerGame;
            _gameSettings.EngineLevel = eEngineLevel.Novice;
            _gameSettings.FirstToMove = eFirstToMove.Player;

            Success = false;

            // create commands
            StartNewGameCommand = new AutoDelegateCommand(() => { StartNewGame(); });
            CancelFormCommand = new AutoDelegateCommand(() => { CancelForm(); });
        }

        /// <summary>
        /// Binding Properties
        /// </summary>

        public AutoDelegateCommand StartNewGameCommand
        { get; set; }

        public AutoDelegateCommand CancelFormCommand
        { get; set; }

               
        public bool IsComputerGame
        {
            get
            {
                return _gameSettings.GameMode == eGameMode.ComputerGame;
            }
            set
            {
                _gameSettings.GameMode = value ? eGameMode.ComputerGame : eGameMode.TwoPlayerGame;
                RaisePropertyChanged("IsComputerGame");
            }
        }

        public bool IsNoviceLevel
        {
            get
            {
                return _gameSettings.EngineLevel == eEngineLevel.Novice;
            }
            set
            {
                _gameSettings.EngineLevel = value ? eEngineLevel.Novice : eEngineLevel.Advanced;
            }
        }

        public bool EngineFirstToMove
        {
            get
            {
                return _gameSettings.FirstToMove == eFirstToMove.Computer;
            }
            set
            {
                _gameSettings.FirstToMove = value ? eFirstToMove.Computer : eFirstToMove.Player;
            }
        }

        public bool PlayerFirstToMove
        {
            get
            {
                return !EngineFirstToMove;
            }
            set
            {
                EngineFirstToMove = !value;
            }
        }

        private void StartNewGame()
        {
            Success = true;
            _events.GetEvent<CloseForm>().Publish(this);
        }

        private void CancelForm()
        {
            Success = false;
            _events.GetEvent<CloseForm>().Publish(this);
        }

   

    }
}
